package com.elink.enginerring.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elink.enginerring.co.BaseCo;
import com.elink.enginerring.common.CoinEnum;
import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.common.HessionEnum;
import com.elink.enginerring.common.HessionInfluenceEnum;
import com.elink.enginerring.common.Pagination;
import com.elink.enginerring.dao.PostbarMapper;
import com.elink.enginerring.dao.PraiseMapper;
import com.elink.enginerring.dao.ReplyMapper;
import com.elink.enginerring.domain.Postbar;
import com.elink.enginerring.domain.Praise;
import com.elink.enginerring.domain.Reply;
import com.elink.enginerring.hessian.CoinHessianService;
import com.elink.enginerring.hessian.PersonDynamicHessianService;
import com.elink.enginerring.service.ReplyService;
import com.elink.enginerring.util.JSONUtil;

@Service
public class ReplyServiceIml  implements ReplyService{
	@Autowired
	private ReplyMapper replyMapper;
	@Autowired
	private PostbarMapper postbarMapper;
	@Autowired
	private PraiseMapper praiseMapper;
	@Autowired
	private PersonDynamicHessianService  personDynamicHessianService;
	@Autowired
	private CoinHessianService coinHessianService;

	@Override
	public Map<String, Object> addReply(Reply reply) {
		//校验，一个用户一个问题只能够回复一次
		int count = replyMapper.select(reply.getUserId(),reply.getPostbarId());
		if(count==0){	
			reply.setReplyTime(new Date());
			replyMapper.insertSelective(reply);
			
			//贴吧更新回复数
			Postbar p = postbarMapper.selectByPrimaryKey(reply.getPostbarId());
			p.setReplyCount(p.getReplyCount()+1);
			postbarMapper.updateByPrimaryKey(p);
			
			//远程备份
			try {
				personDynamicHessianService.addPersonDynamic(HessionEnum.ADDREPLY.getIndex(),0,p.getTitle(),
						reply.getContent(),reply.getUserId(),reply.getPostbarId(),p.getUserId());
			} catch (Exception e) {
				  Logger logger = Logger.getLogger(ReplyServiceIml.class);
				  logger.info("远程提交回复失败"+e);
			}
	
			//远程添加影响值
			try {
				coinHessianService.modifyInfluenceValue(reply.getPostbarId(),HessionInfluenceEnum.ADDREPLY.getIndex(),reply.getUserId());
			} catch (Exception e) {
				  Logger logger = Logger.getLogger(ReplyServiceIml.class);
				  logger.info("远程提交回复答案影响值失败"+e);
			}
			
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}else{
			return JSONUtil.getJSON(ErrorType.POSTBAR_REPLY_ERROR);
		}
		
	}
	
	@Override
	public Map<String, Object> updateReply(Reply reply) {
		Reply r = replyMapper.selectByUserId(reply.getReplyId(),reply.getUserId());
		if(r != null){
			replyMapper.updateByPrimaryKeySelective(reply);
			//远程更新
			try {
				personDynamicHessianService.modifyPersonDynamic(HessionEnum.MODIFYREPLY.getIndex(),0,r.getContent(),r.getContent(),r.getPostbarId(),r.getUserId());
			} catch (Exception e) {
				  Logger logger = Logger.getLogger(ReplyServiceIml.class);
				  logger.info("远程更新回复失败"+e);
			}
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}else{
			return JSONUtil.getJSON(ErrorType.UPDATE_ERROR);
		}
	}
	
	@Override
	public Map<String, Object> updateSelect(int replyId) {
		Reply r = replyMapper.selectByPrimaryKey(replyId);
		int count = replyMapper.selectCountOfBestAnswer(r.getPostbarId());
		if(count==0){
			replyMapper.updateSelect(replyId);
			//更新问题是否解决
			Postbar post = postbarMapper.selectByPrimaryKey(r.getPostbarId());
			post.setSolve(2);
			postbarMapper.updateByPrimaryKeySelective(post);
			//送积分
			Postbar p = null;
			try {
			    p = postbarMapper.selectByPrimaryKey(r.getPostbarId());
				coinHessianService.modifyCoin(String.valueOf(replyId), CoinEnum.POSTBESTANSWER.getIndex() ,p.getPrice().intValue(), r.getUserId(), p.getUserId());
			} catch (Exception e) {
				  Logger logger = Logger.getLogger(ReplyServiceIml.class);
				  logger.info("问题:"+p.getTitle()+"的最佳答案送积分失败"+e); 
			}
			//远程添加影响值
			try {
				coinHessianService.modifyInfluenceValue(r.getPostbarId(),HessionInfluenceEnum.BESTREPLY.getIndex(),r.getUserId());
			} catch (Exception e) {
				  Logger logger = Logger.getLogger(ReplyServiceIml.class);
				  logger.info("远程提交回复答案影响值失败"+e);
			}
			return JSONUtil.getJSON(ErrorType.SUCCESS);
		}else{
			return JSONUtil.getJSON(ErrorType.REPLY_ERROR);
		}
	}
	
	@Override
	public Map<String, Object> getReplyById(BaseCo co, String userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", replyMapper.selectReplyByUserId(userId,co));
		map.put("Pagination", new Pagination(co, replyMapper.selectReplyCount(userId)));
		return JSONUtil.getJSON(ErrorType.SUCCESS, map);
	}

	@Override
	public Map<String, Object> addPraise(Praise praise) {
		Praise oldPraise = praiseMapper.select(praise.getReplyId(),praise.getUserId());//点赞/顶只能够二选一
		Reply r  = replyMapper.selectInfo(praise.getReplyId(),praise.getUserId());
		if(oldPraise == null){
			if(r != null){//如果这条回复不是本人的，则可以点赞
				praise.setPraiseTime(new Date());
				praiseMapper.insertSelective(praise);
				//更新评论的顶/喝倒彩
				if(praise.getType()==1){//点赞
					r.setPraise(r.getPraise()+1);
				}else{//喝倒彩
					r.setBoo(r.getBoo()+1);
				}
				replyMapper.updateByPrimaryKeySelective(r);
				if(praise.getType()==1){
					return JSONUtil.getJSON(ErrorType.SUCCESS,r.getPraise());
				}else{
					return JSONUtil.getJSON(ErrorType.SUCCESS,r.getBoo());
				}
			}else{
				return JSONUtil.getJSON(ErrorType.COMMENT_PRAISE_ERROR);
			}	
		}else {//
			if(praise.getType() ==oldPraise.getType()){//两次相同的点赞/顶
				//更新评论的顶/喝倒彩
				if(praise.getType()==1){//点赞
					r.setPraise(r.getPraise()-1);
				}else{//喝倒彩
					r.setBoo(r.getBoo()-1);
				}
				replyMapper.updateByPrimaryKeySelective(r);
				//删掉点赞记录
				praiseMapper.del(praise.getReplyId(),praise.getUserId());
				return JSONUtil.getJSON(ErrorType.CANCLE_PRAISE);
			}else{
				return JSONUtil.getJSON(ErrorType.PRAISE_ERROR);
			}
		}
	}
}
